package com.csht.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csht.model.CshtUser;
import com.csht.page.PagerModel;
import com.csht.page.SystemContext;
import com.csht.service.CshtGoodsService;
import com.csht.service.CshtShelvesService;
import com.csht.vo.my.CshtShelvesVo;

/**
 * 已下架的商品controller
 * @author chenhuiyan
 *
 */
@Controller
@RequestMapping(value = "/toMy")
public class ShelvesController {
	
	@Autowired
	private CshtShelvesService cshtShelvesService;
	
	@Autowired
	private CshtGoodsService cshtGoodsService;
	
	/**
	 * 个人中心－已下架的商品页面
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/shelves")
	public String shelves(ModelMap model, HttpServletRequest request) throws Exception{
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser");
		Map<String, Integer> res = new HashMap<String, Integer>();
		res.put("userId", cshtUser.getUserId());
		res.put("messagePageSize", SystemContext.getMessagePageSize());
		res.put("offset", SystemContext.getOffset());
		PagerModel<CshtShelvesVo> pagerModel = cshtShelvesService.getShelvesByUserId(res);
		model.addAttribute("pagerModel", pagerModel);
		return "/toMy/shelves";
	}
	
	/**
	 * 重新上架
	 * @param shelvesId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/shelvesGoodsToSell")
	public Map<String, Object> shelvesGoodsToSell(@RequestParam(name = "shelvesId") Integer shelvesId) throws Exception{
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("result", false);
		Integer goodsId = cshtShelvesService.selectShelvesToGoodsIdById(shelvesId); //查询到原商品id
		cshtShelvesService.deleteShelvesGoodsById(shelvesId); //删除记录
		cshtGoodsService.updateGoodsStateSellById(goodsId); //根据商品id更新goods_state为"出售中"
		res.put("result", true);
		return res;
	}

}
